// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Router from 'vue-router'
import App from './App'
import Home from './pages/home/Home'
import Login from './pages/home/login'
import ElementUI from 'element-ui';
import user from './pages/user/user'
import role from './pages/user/role'
import menu from './pages/user/menu'
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
import z_base from './common/base'
import store from './store/store.js'
import {Message} from 'element-ui'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
//axios.defaults.baseURL = 'http://localhost:8081/'
Vue.prototype.$http = axios;
Vue.use(z_base);
Vue.use(Router);
Vue.use(ElementUI);

// axios.interceptors.request.use(
//   config => {
//       if (process.env.NODE_ENV=='production') {  
//       //  axios.defaults.baseURL = 'http://localhost:8081/'
//       console.log(config);
      
//         // console.log(config.url);
//         //   config.url="http://localhost:8081"+config.url.replace('','');
//       }
//       return config;
//   },
//   err => {
//       return Promise.reject(err);
//   });
// http response 服务器响应拦截器，这里拦截401错误，并重新跳入登页重新获取token
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 302:
        case 401:
          // 这里写清除token的代码
          router.replace({
            path: '/login',
            query: { redirect: router.currentRoute.fullPath }//登录成功后跳入浏览的当前页面
          })
          break;
        case 403:
          Message.error({message: '权限不足,请联系管理员!'});
          break;
      }
    }
    return Promise.reject(error.response.data)
  });
const router = new Router({
  routes: [
    { path: '/', redirect: '/home' },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      children: [
        { path: '/user', component: user },
        { path: '/role', component: role },
        { path: '/menu', component: menu },
      ]
    },
    { path: '/login', component: Login }

  ]
})

new Vue({
  el: '#app',
  store,
  router,
  components: { App },
  template: '<App/>',
  methods: {
  },
  mounted() {
   console.log(process.env.NODE_ENV);
   
  },
})
/// 路由权限验证
router.beforeEach((to, from, next) => {
  console.log(to.path);
  if (to.path === '/login') {
    store.state.U_tk="";
    sessionStorage.removeItem("U_tk")
    next();
  } else {
    if (store.state.U_tk || sessionStorage.getItem("U_tk")) {
      next();
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    }
  }
 
});